package com.ag.dgm.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.apache.taglibs.standard.tag.common.core.Util;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Service;

import com.ag.bean.TbEvent;
import com.ag.bean.TbEventReport;
import com.ag.bean.TbEventReportAudit;
import com.ag.bean.TbEventStatus;
import com.ag.bean.TbUser;
import com.ag.bean.TbUserRole;
import com.ag.common.IDUtil;
import com.ag.common.StringUtil;
import com.ag.dao.TbEventDAO;
import com.ag.dao.TbEventReportAuditDAO;
import com.ag.dao.TbEventStatusDAO;
import com.ag.dao.TbUserRoleDAO;
import com.ag.dgm.dao.BaseDAOdgm;
import com.ag.dgm.dao.TbEventReportDAO;
import com.ag.dgm.service.IEventReportService;


/**
 * 
 * @author dgm
 *	EventReportAction的serviceimpl
 */
@Service("eventReportServiceImpl")
public class EventReportServiceImpl implements IEventReportService {

	@Resource(name="TbEventReportAuditDAO")
	private TbEventReportAuditDAO tbEventReportAuditDAO;			//事件报告审核dao
	@Resource(name = "DGMtbEventDAO")
	private TbEventDAO tbEventDAO;									//事件dao
	
	@Resource(name="DGMTbEventReportDAO")
	private TbEventReportDAO tbEventReportDAO;			//事件报告dao
	
	private TbEventReport tbBean = new TbEventReport();

	@Resource(name="TbEventStatusDAO")								
	private TbEventStatusDAO eventStatusDAO;							//事件状态dao
	
	@Resource(name="TbUserRoleDAO")
	private TbUserRoleDAO tbUserRoleDAO;								//用户角色表
	
	@Resource(name="baseDAOdgm")
	private BaseDAOdgm baseDAOdgm;

	/*提交报告，同时回收事件角色*/
	public void submitEventReport(TbUser tbUser, String eventReportString,
			TbEvent tbEvent) {
		//提交报告
		tbBean.setEId(tbEvent.getEId());
		tbBean.setErContent(eventReportString);
		tbBean.setErDate(new Date());
		tbBean.setTbUser(tbUser);
		tbBean.setTbEvent(tbEvent);
		tbEventReportDAO.attachDirty(tbBean);
		
		//设状态为办结
		TbEventStatus tbEventStatus  = new TbEventStatus();
		tbEventStatus.setTbesId("1017");
		tbEvent.setTbEventStatus(tbEventStatus);
		
		TbUser tbUser2=null; 				
		tbEvent.setTbUserByTbUId(tbUser2);//把指挥长一栏设为null
		tbEventDAO.attachDirty(tbEvent);
		
		//回收事件角色，即删除所以事件的角色，针对tb_user_role表
		tbEventReportDAO.deleteEventRole(tbEvent.getEId());
	}

	/**
	 * 通过事件load事件办结报告
	 * @param tbEvent 事件 
	 * @return abc 		事件办结报告
	 */
	@Override
	public String loadEventReportString(TbEvent tbEvent) {
		System.out.println("这是loadEventReportString 方法");
		String eventReportIdString = tbEvent.getEId();
		 String abc = tbEventReportDAO.findById(eventReportIdString).getErContent();
		 return abc;
	}

	/**
	 * 事件的审核
	 * 对审核表的插入
	 * @param auditContent			审核报告
	 * @param tbUser				审核人
	 * @param tbEventIdString		针对事件的id
	 * @param auditAgree			是否审核成功
	 * 
	 */
	public void auditReport(String auditContent, TbUser tbUser,
			String tbEventIdString,String auditAgree) {
		System.out.println("这是auditReport_ServiceImpl方法");
		/*
		 * 存auditAgree
		 */
		TbEventReport tbEventReport = tbEventReportDAO.findById(tbEventIdString);
		TbEventReportAudit tbEventReportAudit = new TbEventReportAudit();
		
		tbEventReportAudit.setTbEventReport(tbEventReport);
		tbEventReportAudit.setTbUser(tbUser);
		tbEventReportAudit.setEraContent(auditContent);
		tbEventReportAudit.setEraAgree(auditAgree);
		tbEventReportAudit.setEraTime(new Date());
		
		tbEventReportAuditDAO.save(tbEventReportAudit);
		/*
		 * 设置状态
		 */
		if(auditAgree.equals("Y")){
		TbEventStatus tbEventStatus  = new TbEventStatus();
		tbEventStatus.setTbesId("1017");
		TbEvent tbEvent = tbEventDAO.findById(tbEventIdString);
		tbEvent.setTbEventStatus(tbEventStatus);
		tbEventDAO.attachDirty(tbEvent);
		}
	}

	/**
	 * 检查事件状态是否为1016,针对事件表
	 * @param thEventIdString 事件id
	 * @return {@link Boolean} 返回状态是否对
	 */
	@Override
	public boolean checkStatus(String tbEventIdString) {
		System.out.println("这是checkStatus_serviceImpl方法");
		TbEvent tb=  tbEventDAO.findById(tbEventIdString);
		if(tb.getTbEventStatus().getTbesId().equals("1016")){
			
			return true;
		}
		else return false;
	}

	/**
	 * 传入一个事件id，把事件的状态改为归档1018
	 * @param tbEventIdString   事件id
	 * 
	 */
	@Override
	public void updateEventEnd(String tbEventIdString, TbUser tbUser) {
		System.out.println("这是updateEventEnd_serviceImpl方法");
		TbEventStatus tbEventStatus = eventStatusDAO.findById("1018");
		TbEvent instance = tbEventDAO.findById(tbEventIdString);
		String bString = "已归档，归档人:"+tbUser.getUName();
		Date date = new Date();
		bString+=",归档时间:"+date;
		System.out.println("******************************"+bString);
		instance.setTbEventStatus(tbEventStatus);
		instance.setB(bString);
		try {
			tbEventDAO.attachDirty(instance);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void deleteEventcommander(String tbEventIdString) {
		System.out.println("这是deleteEventcommander方法");
		String hql= "delete TbUserRole t where t.tbEvent.EId = "+tbEventIdString+" and t.tbRole.RId =1007";
		baseDAOdgm.queryHQLUpdate(hql);
		
	}






}
